<template>
  <div>
    <div class="inputStr">
      <el-button @click="downloadTemplate" style="margin: 10px" plain type="primary">模板下载</el-button>
      <el-button @click="centerDialogVisible3=true" style="margin: 10px" plain type="info">批量导入<i
          class="el-icon-upload el-icon--right"></i></el-button>
      <el-button @click="showAdd" style="margin: 10px" type="primary">添加</el-button>
      <el-select clearable style="width: 150px;margin-right: 30px;" v-model="pagination.categoryId"
                 placeholder="请选择试题类型">
        <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-input v-model="pagination.queryContent" clearable style="width: 150px" placeholder="请输入标题"></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin: 15px" @click="queryStr">搜索</el-button>
    </div>
    <div v-loading="loading">
      <el-table
          :data="questionList"
          border
          style="width: 100%">
        <el-table-column
            fixed
            align="center"
            :show-overflow-tooltip="true"
            prop="questionContent"
            label="题目内容">
        </el-table-column>
        <el-table-column
            align="center"
            label="题目类型">
          <template slot-scope="scope">
            <span
                v-html="scope.row.questionType==1?'单选题':scope.row.questionType==2?'多选题':scope.row.questionType==3?'判断题':'简答题'"></span>
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            label="创建人">
          <template slot-scope="scope">
            <span v-html="scope.row.user.realName"></span>
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            prop="createTime"
            label="创建时间">
        </el-table-column>
        <el-table-column
            align="center"
            fixed="right"
            label="操作">
          <template slot-scope="scope">
            <el-button @click="detailsShow(scope.row)" type="text" size="small">查看</el-button>
            <el-button type="text" @click="deleteById(scope.row.id)" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="padding-top: 40px">
      <el-pagination
          class="pagiantion"
          :total="total"
          @current-change="handleSelectionChange"
          :current-page="pagination.currentPage"
          :page-size="pagination.pageSize"
          layout="total, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>

    <el-dialog
        title="添加试题"
        :visible.sync="centerDialogVisible2"
        width="30%"
        center>
      <div style="text-align: center">
        <el-select clearable style="width: 150px;margin-right: 30px;" v-model="question.questionType"
                   placeholder="请选择试题类型">
          <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
        <div v-show="question.questionType" style="margin-top: 20px">
          <span style="font-weight: bold">题目:</span>
          <el-input
              type="textarea"
              autosize
              style="width: 220px"
              maxlength="100"
              show-word-limit
              placeholder="请输入内容"
              v-model="question.questionContent"
              clearable>
          </el-input>

          <div style="margin-top: 20px" v-show="question.questionType==1||question.questionType==2">
            <span style="font-weight: bold">A、</span>
            <el-input
                style="width: 170px"
                placeholder="请输入选项A内容"
                v-model="question.optionA"
                clearable>
            </el-input>
            <br>
            <span style="font-weight: bold">B、</span>
            <el-input
                style="width: 170px"
                placeholder="请输入选项B内容"
                v-model="question.optionB"
                clearable>
            </el-input>
            <br>
            <span style="font-weight: bold">C、</span>
            <el-input
                style="width: 170px"
                placeholder="请输入选项C内容"
                v-model="question.optionC"
                clearable>
            </el-input>
            <br>
            <span style="font-weight: bold">D、</span>
            <el-input
                style="width: 170px"
                placeholder="请输入选项D内容"
                v-model="question.optionD"
                clearable>
            </el-input>
            <br>
          </div>

          <div style="margin-top: 20px">
            <span style="font-weight: bold">参考答案:</span>
            <div style="margin-top: 10px" v-show="question.questionType==1">
              <el-radio v-model="question.radioAnswer" label="A">A</el-radio>
              <el-radio v-model="question.radioAnswer" label="B">B</el-radio>
              <el-radio v-model="question.radioAnswer" label="C">C</el-radio>
              <el-radio v-model="question.radioAnswer" label="D">D</el-radio>
            </div>
            <div style="margin-top: 10px" v-show="question.questionType==2">
              <el-checkbox-group @change="changeSelect" v-model="checkList">
                <el-checkbox label="A"></el-checkbox>
                <el-checkbox label="B"></el-checkbox>
                <el-checkbox label="C"></el-checkbox>
                <el-checkbox label="D"></el-checkbox>
              </el-checkbox-group>
            </div>
            <div style="margin-top: 10px" v-show="question.questionType==3">
              <el-radio v-model="question.trueFalse" label=1>正确</el-radio>
              <el-radio v-model="question.trueFalse" label=0>错误</el-radio>
            </div>
            <div style="margin-top: 10px" v-show="question.questionType==4">
              <el-input
                  type="textarea"
                  :rows="2"
                  maxlength="200"
                  show-word-limit
                  placeholder="请输入简答题答案"
                  v-model="question.answerContent">
              </el-input>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible2 = false">取 消</el-button>
    <el-button type="primary" @click="saveQuestion">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
        title="试题导入"
        :visible.sync="centerDialogVisible3"
        width="35%"
        center>
      <div style="text-align:center;" v-loading="loading">
        <el-upload
            class="upload-demo"
            ref="upload"
            drag
            :before-upload="beforeAvatarUpload"
            :on-success="handleAvatarSuccess"
            :on-exceed="handleExceed"
            :limit="1"
            :auto-upload="false"
            :action="this.api_host+'/question/imports/'+this.username"
            multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible3 = false">取 消</el-button>
    <el-button type="primary" @click="submits">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
        title="试题详情"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
      <div style="text-align: center">
        <span
            style="font-weight: bold">题目:</span>{{
          question.questionContent
        }}({{
          question.questionType == 1 ? '单选题' : question.questionType == 2 ? '多选题' : question.questionType == 3 ? '判断题' : '简答题'
        }})<br>
        <div v-show="question.questionType==1 || question.questionType==2" style="margin-top: 10px">
          <span style="margin-right: 70px" v-html="'A、'+question.optionA"></span>
          <span v-html="'B、'+question.optionB"></span><br>
          <span style="margin-right: 70px" v-html="'C、'+question.optionC"></span>
          <span v-html="'D、'+question.optionD"></span><br><br>
          <span v-if="question.questionType==1" style="font-weight: bold;">参考答案:</span>{{ question.radioAnswer }}
          <span v-if="question.questionType==2" style="font-weight: bold;">参考答案:</span>{{ question.selectAnswer }}
        </div>

        <div v-show="question.questionType==3" style="margin-top: 10px">
          <span style="font-weight: bold;">参考答案:</span>{{ question.trueFalse == 1 ? '正确' : '错误' }}
        </div>

        <div v-show="question.questionType==4" style="margin-top: 10px">
          <span style="font-weight: bold;">参考答案:</span>{{ question.answerContent }}
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="centerDialogVisible = false">关 闭</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "QuestionManagement",
  data() {
    return {
      loading: false,
      centerDialogVisible: false,
      centerDialogVisible2: false,
      centerDialogVisible3: false,
      total: 0,
      api_host: "",
      username: "",
      checkList: [],  //多选题选中的答案
      question: {},
      questionList: [],
      categoryList: [
        {
          id: 1,
          name: "单选题"
        },
        {
          id: 2,
          name: "多选题"
        }, {
          id: 3,
          name: "判断题"
        }, {
          id: 4,
          name: "简答题"
        }],
      pagination: {
        currentPage: 1,
        pageSize: 5,
        queryContent: "",
        categoryId: null
      },
    }
  },
  mounted() {
    //获取api地址
    this.api_host = this.$global_msg.api_host;
    this.username = sessionStorage.getItem("username");
    this.findPage();
  },
  methods: {
    beforeAvatarUpload() {
      this.loading = true;
    },
    handleAvatarSuccess(res, file) {
      this.$refs.upload.clearFiles();
      if (res.statusCode == 200) {
        this.loading = false;
        this.centerDialogVisible3 = false;
        this.$message({
          message: res.message,
          type: 'success'
        });
        this.findPage();
      } else {
        this.loading = false;
        this.centerDialogVisible3 = false;
        this.$message.error(res.message);
      }

    },
    submits() {
      this.$refs.upload.submit();
    },
    downloadTemplate() {
      this.$confirm('确定下载模板吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let open = window.open(this.api_host + "/question/downloadTemplate", "_top");
        open.close();
        this.$message({
          type: 'success',
          message: '下载成功!'
        });
      })
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    saveQuestion() {
      if (this.question.questionType == null || this.question.questionType == '') {
        this.$message.info("请选择试题类型");
        return;
      }
      if (this.question.questionContent == null || this.question.questionContent == '') {
        this.$message.info("请选择试题内容");
        return;
      }
      if (this.question.questionType == 1 || this.question.questionType == 2) {
        if (this.question.optionA == null || this.question.optionA == '' || this.question.optionB == null || this.question.optionB == '' || this.question.optionC == null || this.question.optionC == '' || this.question.optionD == null || this.question.optionD == '') {
          this.$message.info("请完善A、B、C、D选项内容");
          return;
        }
        if (this.question.questionType == 1) {
          if (this.question.radioAnswer == null || this.question.radioAnswer == '') {
            this.$message.info("请选择单选题答案");
            return;
          }
        } else {
          if (this.question.selectAnswerList == null || this.question.selectAnswerList.length == 0 || this.question.selectAnswerList == '') {
            this.$message.info("请至少选择一个多选题答案");
            return;
          }
        }
      }
      if (this.question.questionType == 3) {
        if (this.question.trueFalse == null) {
          this.$message.info("请选择判断题答案");
          return;
        }
      }
      if (this.question.questionType == 4) {
        if (this.question.answerContent == null || this.question.answerContent == '' || this.question.answerContent.length == 0) {
          this.$message.info("请输入简答题答案");
          return;
        }
      }

      //发送请求
      this.axios.post("/question/save", this.question).then(resp => {
        if (resp.data.statusCode == 200) {
          this.centerDialogVisible2 = false;
          this.$message({
            type: 'success',
            message: resp.data.message
          });
          this.findPage();
        }
      })
    },
    changeSelect() {
      this.question.selectAnswerList = this.checkList;
    },
    showAdd() {
      this.question = {};
      this.centerDialogVisible2 = true;
    },
    deleteById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.delete("/question/deleteById/" + id).then(resp => {
          if (resp.data.statusCode == 200) {
            this.$message({
              type: 'success',
              message: resp.data.message
            });
            this.findPage();
          }
        })
      })
    },
    // closeClear(done) {
    //   //this.question = {};
    //   done();
    // },
    detailsShow(row) {
      this.question = row;
      this.centerDialogVisible = true;
    },
    queryStr() {
      this.findPage();
    },
    handleSelectionChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.findPage();
    },
    findPage() {
      this.loading = true;
      this.axios.post("/question/findPage", this.pagination).then(resp => {
        if (resp.data.statusCode == 200) {
          this.total = resp.data.data.total;
          //this.pagination.toal = resp.data.data.total;
          this.questionList = resp.data.data.data;
        }
        this.loading = false;
      })
    },
  }
}
</script>

<style scoped>
.inputStr {
  width: 800px;
  margin-left: 260px;
}
</style>
